<template>
  <page-form :x-uri="uri" :x-tabs="tabs" :bind-submit="submit" :disable-submit-btn="disableSubmitBtn" />
</template>

<script>
import PageForm from '@public/template/page_form.vue';
import { mapState } from 'vuex';

export default {
  components: {
    PageForm
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'admin/system/config/category',
      tabs: [
        {
          title: '分类配置',
          uri: '/system/config/category',
          meta: {
            permission: 'system.config.category'
          }
        }
      ],
      disableSubmitBtn: false
    };
  },
  computed: {
    ...mapState('pageForm', {
      pageData: 'responseData'
    })
  },
  methods: {
    submit: function(form) {
      this.$box
        .confirm('切换状态后，相关数据的分类可能需要重新编辑，确定要操作吗')
        .then(() => {
          this.disableSubmitBtn = true;
          this.$http.send(
            this.pageData.saveMethod,
            this.pageData.savePostUrl,
            {
              ...form
            },
            response => {
              this.$message.success(response.msg, _ => {
                if (this.redirect) {
                  this.$router.push({ path: this.redirect });
                } else {
                  this.reload();
                }
              });
            },
            response => {
              this.disableSubmit = false;
              this.$message.error(response.msg);
            }
          );
        })
        .catch(() => {
          this.disableSubmitBtn = false;
        });
    }
  }
};
</script>
